local AIO = AIO or require("AIO")
if AIO.AddAddon() then return end
local LeoHandlers = AIO.AddHandlers("unbindsys", {})
local buttonstr = "手动解绑副本"
local firststr = "|cffffff00手动解绑副本系统|r\n\n在下方输入正确的绑定编号\n（在团队信息内可查看绑定编号）"
local errorstr = "|cffff0000请输入有效内容！！|r"

function OnStaticPopupDialogsAccpet(instanceID, instanceName, instanceDifficulty)
	AIO.Handle("unbindsys", "OnStaticPopupDialogsAccpet", instanceID, instanceName, instanceDifficulty-1)
end

local function SetStaticPopupDialogs(index, instanceName, instanceID, instanceDifficulty)
	StaticPopupDialogs["ButtonunbindinstanceSys"..index] = {
		text = string.format("确定要解除|cffffff00<%s>|r的绑定状态吗？", instanceName),
		button1 = ACCEPT,
		button2 = CANCEL,
		OnAccept = function(self)
			OnStaticPopupDialogsAccpet(instanceID, instanceName, instanceDifficulty)
			RaidFrame:Hide()
			RaidFrame:Show()
		end,	
		timeout = 0,
		exclusive = true,
		whileDead = false,
		hideOnEscape = true,
	};
end

local function UpdateSelectedIndex()
	local scrollFrame = RaidInfoScrollFrame;
	local savedInstances = GetNumSavedInstances();
	local offset = HybridScrollFrame_GetOffset(scrollFrame);
	local buttons = scrollFrame.buttons;
	local numButtons = #buttons;
	for i=1, numButtons do
		local index = i + offset;
		if ( index <=  savedInstances) then
			local instanceName, instanceID, instanceReset, instanceDifficulty, locked, extended, instanceIDMostSig, isRaid, maxPlayers, difficultyName = GetSavedInstanceInfo(index);
			local dialogName = instanceName..difficultyName
			SetStaticPopupDialogs(index, dialogName, instanceID, instanceDifficulty)
			local frame = _G["RaidInfoScrollFrameButton"..index]
			frame:RegisterForClicks("LeftButtonUp", "RightButtonUp");
			frame:SetScript("OnClick", function(self,button) if (button == "RightButton") then StaticPopup_Show("ButtonunbindinstanceSys"..index) else RaidInfoInstance_OnClick(self) end end)
			frame:SetScript("OnEnter", function(self) 
			GameTooltip:SetOwner(self, "ANCHOR_RIGHT");
			GameTooltip:SetText(self.name:GetText());
			GameTooltip:AddLine(self.difficulty:GetText(), HIGHLIGHT_FONT_COLOR.r, HIGHLIGHT_FONT_COLOR.g, HIGHLIGHT_FONT_COLOR.b);
			GameTooltip:AddLine(format(INSTANCE_ID, self.instanceID), HIGHLIGHT_FONT_COLOR.r, HIGHLIGHT_FONT_COLOR.g, HIGHLIGHT_FONT_COLOR.b);
			GameTooltip:AddLine("点击鼠标右键可进行解绑操作", 0, 255, 0);
			GameTooltip:Show(); end)
		end	
	end
end
RaidFrame:HookScript("OnEvent", function(self) UpdateSelectedIndex() end)

local function UnbindTitleButton(name, parent)
	local button = CreateFrame("Button", name, parent)
	button:SetNormalFontObject(GameFontNormal)
	button:SetHighlightFontObject(GameFontHighlight)
	button:SetDisabledFontObject(GameFontDisable)
	
	local texture = button:CreateTexture()
	texture:SetTexture("Interface\\PAPERDOLLINFOFRAME\\UI-Character-Skills-BarBorderHighlight")
	texture:SetTexCoord(0, 1, 0, 1)
	texture:SetAllPoints(button)
	button.normal=texture
	button:SetNormalTexture(texture)

	texture = button:CreateTexture()
	texture:SetTexture("Interface\\PAPERDOLLINFOFRAME\\UI-Character-Skills-BarBorderHighlight")
	texture:SetTexCoord(0, 1, 0, 1)
	texture:SetAllPoints(button)
	button.pushed=texture
	button:SetPushedTexture(texture)
	
	texture = button:CreateTexture()
	texture:SetTexture("Interface\\PAPERDOLLINFOFRAME\\UI-Character-Tab-Highlight")
	texture:SetTexCoord(0, 1, 0, 1)
	texture:SetAllPoints(button)
	button:SetHighlightTexture(texture)
	return button
end

local button = _G["ButtonunbindinstanceSys_All"] or UnbindTitleButton("ButtonunbindinstanceSys_All", RaidInfoFrame)
button:SetText("所有副本解绑")
button:SetSize(128,64)
button:SetPoint("BOTTOMRIGHT",0,-50)
button:SetScript("OnClick", function () StaticPopup_Show("ButtonunbindinstanceSys_All") end)

StaticPopupDialogs["ButtonunbindinstanceSys_All"] = {
	text = "确定要解除所有副本的绑定状态？",
	button1 = ACCEPT,
	button2 = CANCEL,
	OnAccept = function(self)
		OnStaticPopupDialogsAccpet(-1, "ALL", -1)
		RaidFrame:Hide()
		RaidFrame:Show()
	end,	
	timeout = 0,
	exclusive = true,
	whileDead = false,
	hideOnEscape = true,
};